package com.awaken.content.controller;

import com.awaken.content.models.dto.LoginResultDTO;
import com.awaken.content.models.request.LoginRequest;
import com.awaken.content.models.request.RegisterRequest;
import com.awaken.content.models.request.SendVerificationCodeRequest;
import com.awaken.content.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
@CrossOrigin(origins = "*")
public class AuthController {
    
    @Autowired
    private AuthService authService;
    
    @PostMapping("/login")
    public LoginResultDTO login(@RequestBody LoginRequest request) {
        return authService.login(request);
    }
    
    @PostMapping("/register")
    public LoginResultDTO register(@RequestBody RegisterRequest request) {
        return authService.register(request);
    }
    
    @PostMapping("/logout")
    public Map<String, Object> logout(@RequestHeader("Authorization") String sessionToken) {
        authService.logout(sessionToken.replace("Bearer ", ""));
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "退出登录成功");
        return result;
    }
    
    @PostMapping("/send-code")
    public Map<String, Object> sendVerificationCode(@RequestBody SendVerificationCodeRequest request) {
        boolean success = authService.sendVerificationCode(request);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        result.put("message", success ? "验证码发送成功" : "验证码发送失败，请稍后重试");
        return result;
    }
    
    @PostMapping("/validate-session")
    public LoginResultDTO validateSession(@RequestHeader("Authorization") String sessionToken) {
        return authService.validateSession(sessionToken.replace("Bearer ", ""));
    }
} 